#This file is part of SocialBlast, an application for updating many social services at once.
#Copyright (C) 2007  Jon Bohlinger, Mike Bruder, Jon Mierow, Thomas Knoll, Jason DeBoer-Moran

#SocialBlast is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.

#SocialBlast is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
import wx, os
import gtk, dbus, gobject, dbus.glib
import urllib2, urllib, base64, simplejson 
import re, sys
ID_SUBMIT=101
ID_SETTINGS=110
class MainWindow(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, wx.ID_ANY, title, size=(340,140))
        self.control = wx.StaticText(self, -1, "Enter your status update: ", (20,25))
	self.control.SetFont(wx.Font(10, wx.NORMAL, wx.NORMAL , wx.BOLD))
	self.inputBox = wx.TextCtrl(self, -1, '', (20,50), wx.Size(300,25))
	self.Button3 = wx.Button(self, ID_SUBMIT,     ' Submit ', (235,85), wx.DefaultSize).SetDefault()
	self.Button4 = wx.Button(self, ID_SETTINGS,     ' Settings ', (20,85), wx.DefaultSize)
        wx.EVT_BUTTON(self, ID_SUBMIT, self.SubmitButton)
	wx.EVT_BUTTON(self, ID_SETTINGS, self.SettingsButton)
        self.Show(True)

    def SubmitButton(self, event):
	status_update = self.inputBox.GetValue()
	# Retrieving user information from text file
	file = open("soc_auth", "r")
	user_data = file.readlines()
	file.close()
	i = 0
	for user in user_data:
		user_data[i] = user[:-1]
		i += 1

	# Get Jaiku authentication info

	juser = user_data[0]
	key = user_data[1]

	# Update Jaiku status
	if juser and key != "":
		try:
			urllib2.urlopen("http://api.jaiku.com/json", urllib.urlencode({"user": juser, "personal_key": key, "message": status_update, "method": "presence.send"}))
		except urllib2.HTTPError:
			pass
	else:
		pass
	
	# Get Tumblr authentication information

	truser = user_data[2]
	trpasswd = user_data[3]

	# Update the Tumblr status
	if truser and trpasswd != "":
		try:
			urllib2.urlopen("http://www.tumblr.com/api/write", urllib.urlencode({"email": truser, "password": trpasswd, "type": "regular", "body": status_update, "generator": "SocialBlast"}))
		except urllib2.HTTPError:
			pass
	else:
		pass

	# Build a Basic authentication handler for Twitter

	authurl = "http://twitter.com"
	protocol = ""
	user = user_data[4]
	passwd = user_data[5]
	if user and passwd != "":
		try:
			passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
			passman.add_password(None, authurl, user, passwd)
			authhandler = urllib2.HTTPBasicAuthHandler(passman)
			opener = urllib2.build_opener(authhandler)
			urllib2.install_opener(opener)
			pagehandle = urllib2.urlopen(protocol + authurl)

			# Update the Twitter status

			urllib2.urlopen("http://twitter.com/statuses/update.json", urllib.urlencode({"status": status_update}))
		except urllib2.HTTPError:
			pass
	else:
		pass
		
	# Build an authentication handler for Facebook

	email = user_data[6]
	password = user_data[7]
	if email and password != "":
		opener = urllib2.build_opener(urllib2.HTTPCookieProcessor ())
		urllib2.install_opener(opener)
		loginRequest = urllib2.Request('https://login.facebook.com/login.php?m&amp;next=http%3A%2F%2Fm.facebook.com%2Fhome.php', 'email=%s&pass=%s&login=Login' % (email, password))

		urllib2.urlopen(loginRequest)
		connection = urllib2.urlopen('http://m.facebook.com/')

		form_id = re.findall('name="post_form_id" value="(\w+)"', connection.read())[0]
	
		# Update Facebook status


		statusRequest = urllib2.Request('http://m.facebook.com/home.php', 'post_form_id=%s&status=%s&update=Update' % (form_id, status_update))
		connection = opener.open(statusRequest)
	else:
		pass

	try:
		# PIDGIN Initiate a connection to the Session Bus
		bus = dbus.SessionBus()

		# PIDGIN Associate Pidgin's D-Bus interface with Python objects
		obj = bus.get_object("im.pidgin.purple.PurpleService", "/im/pidgin/purple/PurpleObject")
		purple = dbus.Interface(obj, "im.pidgin.purple.PurpleInterface")

		# PIDGIN Get the users current status
		pidgin_status = purple.PurpleSavedstatusGetCurrent()

		def set_status(name, kind, message):
  		  # Associate the specified availability message with your current status
  		  purple.PurpleSavedstatusSetMessage(pidgin_status, message)
  		  # Activate the new saved status
  		  purple.PurpleSavedstatusActivate(pidgin_status)

		# PIDGIN update the users status
		set_status("This is a placeholder arg", pidgin_status, status_update)

		# PIDGIN Iterate your status through every active account
		for acctID in purple.PurpleAccountsGetAllActive():
  		  # Retrieve the current status
 		  status = purple.PurpleSavedstatusGetCurrent()
	except:
		pass

	self.inputBox.Clear() #Clear the input box after submitting    
    
    def SettingsButton(self, event):
	os.system("python guisettings.py")

app = wx.App()
frame = MainWindow(None, -1, 'SocialBlast')
app.MainLoop()
